package com.lingxu.api.ztb.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingxu.api.ztb.mapper.ConstructionDrawingDesignMapper;
import com.lingxu.api.ztb.model.ConstructionDrawingDesign;
import com.lingxu.api.ztb.service.IConstructionDrawingDesignService;
import com.lingxu.api.ztb.model.vo.ConstructionDrawingDesignVO;



import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;


import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import java.util.*;

/**
 *  施工图设计表  服务实现类
 *
 * @author admin
 * @since 2025-08-11
 */
@Slf4j
@Service
public class ConstructionDrawingDesignServiceImpl extends ServiceImpl<ConstructionDrawingDesignMapper, ConstructionDrawingDesign>  implements IConstructionDrawingDesignService {

    @Resource
    private ConstructionDrawingDesignMapper constructionDrawingDesignMapper;

    @Transactional(readOnly = true)
    @Override
    public IPage<ConstructionDrawingDesignVO> queryPageList(ConstructionDrawingDesignVO query) {
        Page<ConstructionDrawingDesignVO> page = new Page<ConstructionDrawingDesignVO>(query.getPageNo(), query.getPageSize());
        return constructionDrawingDesignMapper.pageList(page,query);
    }


    @Transactional(readOnly = true)
    @Override
    public List<ConstructionDrawingDesignVO> list(ConstructionDrawingDesignVO query) {
        return constructionDrawingDesignMapper.selectConstructionDrawingDesignList(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Set<String> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            constructionDrawingDesignMapper.deleteBatchIds(ids);
        }
    }

    public ConstructionDrawingDesignVO constructionDrawingDesignInfo(ConstructionDrawingDesignVO query) {
        List<ConstructionDrawingDesignVO> list = constructionDrawingDesignMapper.constructionDrawingDesignInfo(query.getProjectname());
        if (list == null || list.isEmpty()) {
            return null;
        }
        ConstructionDrawingDesignVO response = new ConstructionDrawingDesignVO();
        ConstructionDrawingDesignVO first = list.get(0);
        response.setProjectname(first.getProjectname());       // 项目名称
        response.setProjectcode(first.getProjectcode());       // 项目编号
        response.setApprovalTime(first.getApprovalTime());     // 申报批复时间

        Set<String> designDepartments = new HashSet<>();
        Set<String> designDeptHeads = new HashSet<>();
        Set<String> designUnits = new HashSet<>();
        Set<String> designHeads = new HashSet<>();
        Set<String> approvalUnits = new HashSet<>();

        for (ConstructionDrawingDesignVO project : list) {
            addIfNotNull(designDepartments, project.getDesignDepartment());
            addIfNotNull(designDeptHeads, project.getDesignDeptHead());
            addIfNotNull(designUnits, project.getDesignunit());
            addIfNotNull(designHeads, project.getDesignHead());
            addIfNotNull(approvalUnits, project.getApprovalUnit());
        }

        response.setDesignDepartment(joinSet(designDepartments));
        response.setDesignDeptHead(joinSet(designDeptHeads));
        response.setDesignunit(joinSet(designUnits));
        response.setDesignHead(joinSet(designHeads));
        response.setApprovalUnit(joinSet(approvalUnits));

        return response;
    }

    private void addIfNotNull(Set<String> set, String value) {
        if (value != null && !value.trim().isEmpty()) {
            set.add(value.trim());
        }
    }

    private String joinSet(Set<String> set) {
        if (set == null || set.isEmpty()) {
            return "";
        }
        return String.join("/", set);
    }


}
